package org.abac4j.context;

import java.util.Map;
import java.util.Set;

import org.abac4j.aspect.SecurityCGLIBProxy;
import org.abac4j.scanner.AnnotationScanner;

public class AbacContext {
	
	private static AbacContext instance;
	
	private FunctionRepository methodRepository;
	
	public FunctionRepository getMethodRepository() {
		return methodRepository;
	}

	public void setMethodRepository(FunctionRepository methodRepository) {
		this.methodRepository = methodRepository;
	}

	public AttributeRepository getAttributeRepository() {
		return attributeRepository;
	}

	public void setAttributeRepository(AttributeRepository attributeRepository) {
		this.attributeRepository = attributeRepository;
	}

	private AttributeRepository attributeRepository;
	
	private AbacContext(){
		
		Map<String, Set<String>> annotationMap = new AnnotationScanner().scanAnnotations();
		
		methodRepository = new FunctionRepository(annotationMap);
		attributeRepository = new AttributeRepository();
	}

	public static AbacContext getInstance(){
		if(instance == null){
			instance = new AbacContext();
		}
		return instance;
	}
	
	public static <E> E createSafeObject(E object){
		return SecurityCGLIBProxy.createSecurityInterceptor(object, getInstance());
	}
}